﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace NetDisk.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ScanCodeController : ControllerBase
    {
        private readonly IHttpClientFactory _httpClient;

        public ScanCodeController(IHttpClientFactory httpClient)
        {
            _httpClient = httpClient;
        }

        [HttpGet]
        public async Task<string> Get(string url)
        {
            // http://p.wimanu.com/?{%22t%22:30,%22cid%22:362,%22id%22:122,%22batch%22:%229-202008150002-1%22}
            // http://p.wimanu.com/?{%22t%22:1,%22cid%22:362,%22id%22:1368,%22num%22:%221-202007230171-1%22}
            //const string testUrl = "http://p.wimanu.com/?{%22t%22:30,%22cid%22:362,%22id%22:122,%22batch%22:%229-202008150002-1%22}";

            var http = _httpClient.CreateClient();
            var strResponse = await http.GetStringAsync(url);
            var productModel = Regex.Match(strResponse, "(?<=名称型号：\\</label><span>).*(?=</span>)").Value;

            return string.IsNullOrEmpty(productModel) ? string.Empty : productModel;
        }
    }
}
